package com.jaiky.imagespickers;

import com.jaiky.imagespickers.utils.ResUtil;
import com.jaiky.imagespickers.utils.Utils;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * FolderAdapter
 */
public class FolderAdapter extends BaseItemProvider {
    private Context context;
    private LayoutScatter mLayoutInflater;
    private List<ImageFolder> folderList;
    private final int mImageSize;
    private int lastSelected = 0;
    private final ImagePicker imagePicker;

    public FolderAdapter(Context context, List<ImageFolder> folders) {
        mLayoutInflater = LayoutScatter.getInstance(context);
        this.context = context;
        if (folders != null && folders.size() > 0) {
            folderList = folders;
        } else {
            folderList = new ArrayList<>();
        }
        mImageSize = Utils.getImageItemWidth(context);
        imagePicker = ImagePicker.getInstance();
    }

    /**
     * setData
     *
     * @param folders folderList
     */
    public void setData(List<ImageFolder> folders) {
        if (folders != null && folders.size() > 0) {
            folderList = folders;
        } else {
            folderList.clear();
        }
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return folderList.size();
    }

    @Override
    public ImageFolder getItem(int position) {
        return folderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.parse(ResourceTable.Layout_imageselector_item_folder, parent, false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (holder != null) {
            ImageFolder folder = getItem(position);
            holder.folder_name_text.setText(folder.name);
            holder.image_num_text.setText(ResUtil.getString(context,
                    ResourceTable.String_ip_folder_image_count, folder.images.size()));
            imagePicker.getImageLoader().displayImage((Ability) context,
                    folder.cover.uriSchema, holder.folder_image, mImageSize, mImageSize);
            if (lastSelected == position) {
                holder.indicator.setVisibility(Component.VISIBLE);
            } else {
                holder.indicator.setVisibility(Component.INVISIBLE);
            }
        }

        return convertView;
    }

    static class ViewHolder {
        Image folder_image;
        Text folder_name_text;
        Text image_num_text;
        Image indicator;

        ViewHolder(Component itemView) {
            folder_image = (Image) itemView.findComponentById(ResourceTable.Id_iv_cover);
            folder_name_text = (Text) itemView.findComponentById(ResourceTable.Id_tv_folder_name);
            image_num_text = (Text) itemView.findComponentById(ResourceTable.Id_tv_image_count);
            indicator = (Image) itemView.findComponentById(ResourceTable.Id_iv_folder_check);
            itemView.setTag(this);
        }
    }

    /**
     * getSelectIndex
     *
     * @return lastSelected
     */
    public int getSelectIndex() {
        return lastSelected;
    }

    /**
     * setSelectIndex
     *
     * @param position position
     */
    public void setSelectIndex(int position) {
        if (lastSelected == position){
            return;
        }
        lastSelected = position;
        notifyDataChanged();
    }

}